package com.zhixinglu.spring.boot.thread.util;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * Thread
 * </p>
 *
 * @author weixin
 * @date 2023/3/24 10:28
 */
@Slf4j
public class NamedSingleThread {

    private Thread thread;
    private String threadName;

    public NamedSingleThread(String threadName) {
        this.threadName = threadName;
    }

    public void start(Runnable task) {
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    task.run();
                } catch (Exception e) {
                    log.error("Error executing task: " + e.getMessage(), e);
                }
            }
        });
        thread.setName(threadName);
        thread.start();
    }

    public void stop() {
        thread.interrupt();
    }
}
